import { MongoClient } from "mongodb";
import chalk from "chalk";

import { MONGO_URI, MONGO_DB, SOURCE_COLLECTION } from "./config.js";

function removeZeroWidthChars(str) {
  // 定义正则表达式，匹配各种零宽字符
  const zeroWidthChars = /[\u200B-\u200D\uFEFF]/g;
  return str.replace(zeroWidthChars, "");
}

async function updateData() {
  const client = new MongoClient(MONGO_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  });

  try {
    // 连接到 MongoDB
    await client.connect();
    console.log(chalk.bold.yellow("MongoDB connection connected"));
    const db = client.db(MONGO_DB);
    const sourceCollection = db.collection(SOURCE_COLLECTION);

    // 创建一个游标来遍历集合
    const cursor = sourceCollection.find(
      {
        platform_id: "ft_biganzifanwen",
      },
      { noCursorTimeout: true }
    );
    let i = 0;
    for await (const doc of cursor) {
      const { title, url, content } = doc;
      i++;
      // 去除附件
      //   if (content.indexOf("附件：") > 0) {
      //     console.log(chalk.green(`${title}: ${url}`));
      //     let contents = [];
      //     contents = content.split("附件：");
      //     const updatedDoc = {
      //       ...doc,
      //       content: contents[0],
      //     };
      //     // 更新到 SOURCE_COLLECTION
      //     await sourceCollection.updateOne(
      //       { _id: doc._id },
      //       { $set: updatedDoc }
      //     );
      //   }

      //   添加 type 字段
      const updatedDoc = {
        ...doc,
        title: removeZeroWidthChars(title),
      };

      console.log(`${i}---${updatedDoc.title}`);

      // 更新到 SOURCE_COLLECTION
      await sourceCollection.updateOne({ _id: doc._id }, { $set: updatedDoc });
    }

    console.log(chalk.bold.yellow("Success: all docs updated"));
  } catch (err) {
    console.error(err);
  } finally {
    console.log(chalk.bold.yellow("MongoDB connection closed"));
    await client.close();
  }
}

// 执行数据更新
updateData().catch(console.error);
